﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using System.Threading;

namespace DAO
{
    public class KhoThucPhamDAO
    {
        public string CapNhatKhoThucPham(DataGridView dtg)
        {
            string msg = "";
            SqlConnection conn = null;
            try
            {
                conn = DataProvider2.ConnectDB();
                for (int i = 0; i < dtg.Rows.Count; i++)
                {
                    SqlCommand cmd = new SqlCommand("update KhoThucPham Set KhoiLuongTon = KhoiLuongTon - (@SoLuong*@KhoiLuong) Where ID_ThucPham in(Select ID_ThucPham From KhoThucPham Where ID_ThucPham in (Select ID_ThucPham From Congthuc Where ID_MonAn=@ID_MonAn)");
                    
                    //cmd.Parameters.AddWithValue("@KhoiLuongTon",khoiluong);
                    cmd.Parameters.AddWithValue("@ID_MonAn", dtg.Rows[i].Cells[1].Value);
                    cmd.Parameters.AddWithValue("@SoLuong", dtg.Rows[i].Cells[3].Value);
                    cmd.ExecuteNonQuery();
                }
                conn.Close();
                MessageBox.Show("Thêm chi tiết phiếu thành công!");
            }
            catch (SqlException ex)
            {
                msg = ex.Message;
            }
            return msg;
        }

        public string CapNhatNguyenLieu(DataGridView dtg)           
        {                                                                                              
            string mess = "";

            SqlConnection conn = null;
            try
            {
                conn = DataProvider2.ConnectDB();
                for (int i = 0; i < dtg.Rows.Count; i++)
                {
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "usp_CapNhatNguyenLieu";
                    cmd.Parameters.AddWithValue("@ID_MonAn", dtg.Rows[i].Cells[1].Value);
                    cmd.Parameters.AddWithValue("@SoLuong", dtg.Rows[i].Cells[3].Value);
                    cmd.ExecuteNonQuery();
                }
                conn.Close();
            }
            catch (SqlException ex)
            {
                mess = ex.Message;
            }
            finally
            {
                conn.Close();
            }
            return mess;

        }

        public string CapNhatNguyenLieu(DataGridView dtg, int chedo)
        {
            string mess = "";

            SqlConnection conn = null;
            try
            {
                conn = DataProvider2.ConnectDB(chedo);
                for (int i = 0; i < dtg.Rows.Count; i++)
                {
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "usp_CapNhatNLMonAn";
                    cmd.Parameters.AddWithValue("@IDMonAn", dtg.Rows[i].Cells[1].Value);
                    cmd.Parameters.AddWithValue("@SoLuong", dtg.Rows[i].Cells[3].Value);
                    cmd.ExecuteNonQuery();
                }
                conn.Close();
            }
            catch (SqlException ex)
            {
                mess = ex.Message;
            }
            finally
            {
                conn.Close();
            }
            return mess;

        }
    }
}
